<?php

declare(strict_types=1);

namespace App\Controller\Backend;

use App\Controller\AbstractController;
use App\Model\MobileArea;
use App\Request\MobileAreaIdRequest;
use App\Resource\BaseCollection;
use App\Resource\BaseResource;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;

class MobileAreasManage extends AbstractController
{
    public function index(RequestInterface $request, ResponseInterface $response)
    {
        $where = [];

        if ($request->input('mobile_area')) {

            $where[] = ['mobile_area', '=', $request->input('mobile_area')];
        }

        $list = MobileArea::where($where)->orderByDesc('updated_at')->paginate();

        return new BaseCollection($list);
    }

    public function mobileAreaOpen(MobileAreaIdRequest $request, ResponseInterface $response)
    {
        $data = $request->validated();
        $area = MobileArea::find($data['id']);
        $area->is_enable = 1;
        $area->save();
        return new BaseResource();
    }

    public function mobileAreaClose(MobileAreaIdRequest $request, ResponseInterface $response)
    {
        $data = $request->validated();
        $area = MobileArea::find($data['id']);
        $area->is_enable = 2;
        $area->save();
        return new BaseResource();
    }
}
